package com.usefullc.system.configuration.jackson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.io.IOException;
import java.util.Date;

@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper objectMapper() {
        return createObjectMapper();
    }
    private ObjectMapper createObjectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Date.class, new DateSerializer());
//        simpleModule.addSerializer(Boolean.class, new StdScalarSerializer<Boolean>(Boolean.class){
//
//            @Override
//            public void serialize(Boolean value, JsonGenerator gen, SerializerProvider provider) throws IOException {
//                int obj = 0;
//                if(value != null && value){
//                    obj = 1;
//                }
//                gen.writeStartObject(obj);
//            }
//        });
        //忽略null字段
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //允许json属性名不使用双引号
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        //忽略不存在字段
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        simpleModule.addDeserializer(String.class, new StringDeserializer());
//        simpleModule.addDeserializer(Boolean.class, new BooleanDeserializer());
        simpleModule.addDeserializer(Boolean.class, new StdScalarDeserializer<Boolean>(Boolean.class){
            @Override
            public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
                String valueAsString = p.getValueAsString();
                if(StringUtils.equalsIgnoreCase(valueAsString,"1")){
                    return true;
                }else if(StringUtils.equalsIgnoreCase(valueAsString,"0")){
                    return false;
                }
                return BooleanUtils.toBoolean(valueAsString);
            }
        });
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}
